
package com.kugou.game.sdk.receiver;

import android.content.Context;
import android.content.Intent;

import com.kugou.framework.base.BaseBroadcastReceiver;
import com.kugou.game.sdk.core.SDKActions;

/**
 * 描述:监听安装、卸载、覆盖安装应用
 * 
 * @author chenys
 * @since 2013-11-7 下午9:42:26
 */
public class AppReceiver extends BaseBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        // 获取packageName
        String packageName = intent.getDataString();
        if (packageName != null && packageName.contains("package:")) {
            packageName = packageName.substring("package:".length());
        } else {
            packageName = "";
        }

        if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
            // 卸载
            Intent removeIntent = new Intent(SDKActions.ACTION_APP_REMOVE);
            removeIntent.putExtra("packageName", packageName);
            context.sendBroadcast(removeIntent);
        } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
            // 安装
            Intent installIntent = new Intent(SDKActions.ACTION_APP_INSTALL);
            installIntent.putExtra("packageName", packageName);
            context.sendBroadcast(installIntent);
        } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
            // 覆盖安装
            Intent replaceIntent = new Intent(SDKActions.ACTION_APP_REPLACE);
            replaceIntent.putExtra("packageName", packageName);
            context.sendBroadcast(replaceIntent);
        }
    }

}
